{	Problem: http://codeforces.ru/problemset/problem/63/A
	Verdict: Accepted

}
var i,n,k:longint; s,nm,st:string; a:array[1..101] of byte; p:array[1..101] of string;
begin
readln(n);
fillchar(a,sizeof(a),0);
for i:=1 to n do begin
  readln(S);
  k:=pos(' ',s);
  nm:=copy(s,1,k-1);
  st:=copy(s,k+1,length(s)-k);
  p[i]:=nm;
  if st='rat' then a[i]:=1 else
  if (st='child') or (st='woman') then a[i]:=2 else
  if st='man' then a[i]:=3 else a[i]:=4;
end;
for i:=1 to n do if a[i]=1 then writeln(p[i]); 
for i:=1 to n do if a[i]=2 then writeln(p[i]);
for i:=1 to n do if a[i]=3 then writeln(p[i]);
for i:=1 to n do if a[i]=4 then writeln(p[i]);
end.